package org.bear.linkList;

public class AddTwoNumbers {

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head = new ListNode(0);
        ListNode p = head;
        int lastCarry = 0;
        while(l1!=null || l2!=null){
            int cur = lastCarry;
            if (l1==null){
            }else {
                cur += l1.val;
                l1 = l1.next;
            }
            if (l2==null){
            }else  {
                cur += l2.val;
                l2 = l2.next;
            }
            if (cur>9){
                lastCarry = 1;
                cur = cur%10;
            }else  {
                lastCarry = 0;
            }
            p.next = new ListNode(cur);
            p = p.next;
        }
        if (lastCarry>0){
            p.next = new ListNode(lastCarry);
        }
        return head.next;
    }
}
